﻿using Compass.PlanService.Infrastructure;
using Compass.Wasm.Server.Services.Plans;
using Compass.Wasm.Shared.Plans;
using System.ComponentModel.DataAnnotations;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Charts;

namespace Compass.Wasm.Server.Controllers.Plans;

[Route("api/[controller]")]
[ApiController]
[UnitOfWork(typeof(PlanDbContext))]
//[Authorize(Roles = "admin,pmc")]
public class MainPlanController : ControllerBase
{
    private readonly IMainPlanService _service;
    public MainPlanController(IMainPlanService service)
    {
        _service = service;
    }

    #region 标准增删改查
    [HttpGet("All")]
    public async  Task<ApiResponse<IEnumerable<MainPlanDto>>> GetAll() =>await  _service.GetAllAsync();

    [HttpGet("{id}")]
    public async Task<ApiResponse<MainPlanDto>> GetSingle([RequiredGuid] Guid id) =>  await _service.GetSingleAsync(id);

    [HttpPost("Add")]
    public async Task<ApiResponse<MainPlanDto>> Add(MainPlanDto dto) =>await _service.AddAsync(dto);

    [HttpPut("{id}")]
    public async Task<ApiResponse<MainPlanDto>> Update([RequiredGuid] Guid id, MainPlanDto dto) =>await _service.UpdateAsync(id, dto);

    [HttpDelete("{id}")]
    public async Task<ApiResponse<MainPlanDto>> Delete([RequiredGuid] Guid id) =>await _service.DeleteAsync(id);
    #endregion

    #region Blazor扩展
    [HttpGet("Number")]
    public async Task<ApiResponse<MainPlanDto>> GetSingleByNumber(string number) => await _service.GetSingleByNumberAsync(number);


    /// <summary>
    /// 更新主计划状态
    /// </summary>
    [HttpPut("UpdateStatuses/{id}")]
    public async Task<ApiResponse<MainPlanDto>> UpdateStatuses([RequiredGuid] Guid id, MainPlanDto dto) =>await _service.UpdateStatusesAsync(id, dto);

    

    /// <summary>
    /// 根据项目Id查询项目的主计划
    /// </summary>
    [HttpGet("Project/{projectId}")]
    public async Task<ApiResponse<IEnumerable<MainPlanDto>>> GetAllByProjectId([RequiredGuid] Guid projectId) =>await _service.GetAllByProjectIdAsync(projectId);


    /// <summary>
    /// 查询项目汇总结果
    /// </summary>
    [HttpGet("Count")]
    public async Task<ApiResponse<MainPlanCountDto>> GetCount() =>await _service.GetCountAsync();


    #endregion

    #region MAUI
    [HttpGet("Maui/All")]
    public async Task<ApiResponse<IEnumerable<MainPlanDto>>> GetMauiMainPlans(int skip, int take, string? search=null) =>await _service.GetMauiMainPlansAsync(skip, take,search);
    #endregion


    #region Blazor
    [HttpGet("Blazor/All")]
    public async Task<ApiResponse<IEnumerable<MainPlanDto>>> GetBlazorMainPlans(int year,int month,bool annual,bool fromMonth,bool openIssue,bool withIssue) => await _service.GetBlazorMainPlansAsync(year,month,annual,fromMonth,openIssue,withIssue);


    [HttpGet("Blazor/Status")]
    public async Task<ApiResponse<IEnumerable<ChartIntDataDto>>> GetBlazorMainPlanStatus(int year, int month, bool annual) => await _service.GetBlazorMainPlanStatusAsync(year, month, annual);

    [HttpGet("Blazor/DeliveryReliability")]
    public async Task<ApiResponse<IEnumerable<ChartDoubleDataDto>>> GetBlazorDeliveryReliability(int year) => await _service.GetBlazorDeliveryReliabilityAsync(year);
    [HttpGet("Blazor/DeliveryReliabilityYtd")]
    public async Task<ApiResponse<double>> GetBlazorDeliveryReliabilityYtd(int year) => await _service.GetBlazorDeliveryReliabilityYtdAsync(year);

    [HttpGet("Blazor/ProductionCycleTime")]
    public async Task<ApiResponse<IEnumerable<ChartDoubleDataDto>>> GetBlazorProductionCycleTime(int year) => await _service.GetBlazorProductionCycleTimeAsync(year);
    [HttpGet("Blazor/ProductionCycleTimeYtd")]
    public async Task<ApiResponse<double>> GetBlazorProductionCycleTimeYtd(int year) => await _service.GetBlazorProductionCycleTimeYtdAsync(year);

    [HttpGet("Blazor/FactoryCycleTime")]
    public async Task<ApiResponse<IEnumerable<ChartDoubleDataDto>>> GetBlazorFactoryCycleTime(int year) => await _service.GetBlazorFactoryCycleTimeAsync(year);
    [HttpGet("Blazor/FactoryCycleTimeYtd")]
    public async Task<ApiResponse<double>> GetBlazorFactoryCycleTimeYtd(int year) => await _service.GetBlazorFactoryCycleTimeYtdAsync(year);

    [HttpGet("Blazor/OrderCycleTime")]
    public async Task<ApiResponse<IEnumerable<ChartDoubleDataDto>>> GetBlazorOrderCycleTime(int year) => await _service.GetBlazorOrderCycleTimeAsync(year);
    [HttpGet("Blazor/OrderCycleTimeYtd")]
    public async Task<ApiResponse<double>> GetBlazorOrderCycleTimeYtd(int year) => await _service.GetBlazorOrderCycleTimeYtdAsync(year);

    #endregion
}